package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStatisVORedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IStatisVORedisService statisVORedisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        return userInfoService.checkPhone(phone);
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(UserInfo userInfo,String rpassword, String verifyCode) {
        //try {
        //    userInfoService.regist(userInfo, rpassword, verifyCode);
        //} catch(Exception e) {
        //    e.printStackTrace();
        //    return new JsonResult(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        //}
        userInfoService.regist(userInfo, rpassword, verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username, String password) {
        UserInfo user = userInfoService.login(username, password);
        String token = userInfoService.setUserByToken(user);
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("token", token);
        return JsonResult.success(map);
    }



    //@RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user =  userInfoService.getUserByToken(token);
        return JsonResult.success(user);
    }

    @GetMapping("/strategies/favor")
    public Object favor(String sid, String userId) {
        List<String> strategyIds = statisVORedisService.isExistFavorStrategy(sid, userId);
        return JsonResult.success(strategyIds);
    }

}
